const Key = require('../models/Key')
const {getPublicKey} = require('../utils/rsaControl')
exports.getPubKey = async function (req, res, next) {
  /*
   * 本地获取
   * let pubKey = await getPublicKey()
  */

  try {
    let result = await Key.findOne()
    res.json({
      code:20000,
      data: {
        pubKey: result.content
      }
    })

  } catch (err) {
    console.log("-> err123:", err);
    let pubKey = await getPublicKey()
    const model = await Key.create({content: pubKey})
    next(err)
  }
}
// 初始化数据库时候用
exports.addPubKey = async function (req, res, next) {
  try {
    const model = await Key.create(req.body)
    res.send(model)
  } catch (err) {
    next(err)
  }
}
